﻿using System.Collections.Generic;
using System;

namespace FuzzScript
{
    class Program
    {
        static void Main(string[] args)
        {
            Execute();
        }

        /// <summary>
        /// Comparison of fuzzy system and its optimized equivalent
        /// </summary>
        static void Execute()
        {
            try
            {
                FuzzyEngine engine = new FuzzyEngine("..\\..\\System1.txt");
                //FuzzyEngine engine = new FuzzyEngine("..\\..\\test.txt");

                IControlEngine eng = engine.Generate();

                // Create Internal state
                Dictionary<string, double> internalStates = new Dictionary<string, double>();
                internalStates.Add("SENSORRIGHT", 200);
                internalStates.Add("SENSORMIDDLE", 200);
                internalStates.Add("SENSORLEFT", 200);

                // Create control values - initialize all to zero
                Dictionary<string, double> controlActions = new Dictionary<string, double>();
                controlActions.Add("VELOCITYLINEAR", 0.0);
                controlActions.Add("VELOCITYANGULAR", 0.0);


                System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

                double d = 0;
                for (int i = 0; i < 100; i++)
                {
                    stopwatch.Start();
                    engine.Control( internalStates, controlActions);
                    stopwatch.Stop();
                    if (i > 1)
                        d += stopwatch.ElapsedTicks;
                    stopwatch.Reset();
                }
                System.Console.WriteLine("----->" + d.ToString());


                foreach (KeyValuePair<string, double> action in controlActions)
                {
                    System.Console.WriteLine(action.Key + "----->" + action.Value.ToString());
                }

                double d2 = 0;
                for (int i = 0; i < 100; i++)
                {
                    stopwatch.Start();
                    eng.Control(internalStates, controlActions);
                    stopwatch.Stop();
                    if (i > 1)
                        d2 += stopwatch.ElapsedTicks;
                    stopwatch.Reset();
                }
                System.Console.WriteLine("----->" + d2.ToString());

                foreach (KeyValuePair<string, double> action in controlActions)
                {
                    System.Console.WriteLine(action.Key + "----->" + action.Value.ToString());
                }
                System.Console.WriteLine("----->" + (1 - (d2 / d)));
            }
            catch (Exception e)
            {
                System.Console.WriteLine("\nUnable to execute system");
                System.Console.WriteLine(e.Message);
            }
        }
    }
}
